<?php
namespace Admin\Controller;

use Common\Util\UploadHandler;
use Common\Util\Page;
use Common\Controller\AdminbaseController;

class FinancialManagementController extends AdminbaseController {
	
    public function index() {
    	
    	$beauticianId = I('get.id');
    	$start = I('get.start_time');
    	$end = I('get.end_time');
    	
    	$where = array();
    	if ($beauticianId) {
    		$where ['beautician_id'] = $beauticianId;
    	}
    	
    	if ($start || $end) {
    		$where ['created'] [] = array('gt', strtotime($start. " 00:00:00"));
    		if ($end) {
    			$where ['created'] [] = array('lt', strtotime($end. " 00:00:00"));
    		} else {
    			$where ['created'] [] = array('lt', time());
    		}
    	}
    	
    	$incomeModel = M('income_statement');
    	$count = $incomeModel->where($where)->count();
    	 
    	$page = $this->page($count, 20);
    	
    	$list = $incomeModel->where($where)
    		->limit($page->firstRow.','.$page->listRows)
    		->select();
    	$this->assign('Page', $page->show('Admin'));
    	
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function present_record() {
    	
    	$where = array();
    	
    	$card = I('get.card');
    	$name = I('get.beautician_name');
    	if ($card) {
    		$where ['c.card'] = $card;
    	}
    	
    	if ($name) {
    		$where ['b.name'] = array('like', '%'.$name.'%');
    	}
    	
    	$starTime = I('get.start_time');
    	if ($starTime) {
    		$endTime = I('get.end_time');
    		$where ['pr.created'] = array(array('gt', strtotime($starTime.' 00:00:00')));
    		if ($endTime) {
    			$endTimeStamp = strtotime($endTime.' 23:59:59');
    		} else {
    			$endTimeStamp = time();
    		}
    		$where ['pr.created'] [] = array('lt', $endTimeStamp);
    	}
    	
    	$presentModel = M('present_record');
    	$count = $presentModel->alias('pr')
	    	->join('left join '.C('DB_PREFIX').'card as c on pr.card_id=c.id')
	    	->join('left join '.C('DB_PREFIX').'beautician as b on pr.beautician_id=b.id')
    		->where($where)
    	->count();
    	
    	$page = $this->page($count, 20);
    	$this->assign('Page', $page->show('Admin'));
    	
    	$list = $presentModel->alias('pr')
    		->field(array('pr.*', 'c.card', 'c.bank_name', 'c.cardHolder', 'c.card_type', 'b.name'))
    		->join('left join '.C('DB_PREFIX').'card as c on pr.card_id=c.id')
    		->join('left join '.C('DB_PREFIX').'beautician as b on pr.beautician_id=b.id')
    		->where($where)
    		->limit($page->firstRow.','.$page->listRows)
    	->select();
    	
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function refund_record() {
    	$where = array();
    	$where = array();
    	 
    	$card = I('get.card');
    	if ($card) {
    		$where ['c.card'] = $card;
    	}
    	 
    	$starTime = I('get.start_time');
    	if ($starTime) {
    		$endTime = I('get.end_time');
    		$where ['rr.created'] = array(array('gt', strtotime($starTime.' 00:00:00')));
    		if ($endTime) {
    			$endTimeStamp = strtotime($endTime.' 23:59:59');
    		} else {
    			$endTimeStamp = time();
    		}
    		$where ['rr.created'] [] = array('lt', $endTimeStamp);
    	}
    	
    	$refundModel = M('refund_record');
    	$count = $refundModel
    	->alias('rr')
    	->join('left join '.C('DB_PREFIX').'card as c on rr.card_id=c.id')
    	->join('left join '.C('DB_PREFIX').'order as o on rr.order_id=o.id')
    	->where($where)
    	->count();
    	
    	$page = $this->page($count, 20);
    	$this->assign('Page', $page->show('Admin'));
    	
    	$list = $refundModel
    		->field(array('rr.*', 'c.card', 'c.bank_name', 'c.cardHolder', 'c.card_type', 'o.price'))
    		->alias('rr')
    		->join('left join '.C('DB_PREFIX').'card as c on rr.card_id=c.id')
    		->join('left join '.C('DB_PREFIX').'order as o on rr.order_id=o.id')
    		->where($where)
    	->select();
    	
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function settingRefundStatus() {
    	$RefundRecordModel = M('refund_record');
    	$OrderModel = D('Order');
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$where = array('id' => $id);
    		$result = $RefundRecordModel->where($where)->save(array('status' => 1));
    		
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			$refundInfo = $RefundRecordModel->where(array('id' => $id))->find();
    			$OrderModel->where(array('id' => $refundInfo ['order_id']))->save(array('status' => 8));
    			$this->success('success', U('refund_record'));
    		} else {
    			$this->error('fail');
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			 
    			$where = array('id' => $ids[0]);
    			$result = $RefundRecordModel->where($where)->save(array('status' => 1));
    		} else {
    			$where ['id'] = array('in', implode(',', $ids));
    			$result = $RefundRecordModel->where($where)->save(array('status' => 1));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $id) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			
    			$refundInfo = $RefundRecordModel->where(array('id' => $id))->find();
    			$OrderModel->where(array('id' => $refundInfo ['order_id']))->save(array('status' => 8));
    			
//     			$OrderModel->clearCache($orderId);
    		}
    		$this->ajaxReturn(array('status' => 1, 'url' => U('refund_record')));
    	}
    }

    public function settingPresentStatus() {
    	$PresentRecordModel = M('present_record');
    	
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$where = array('id' => $id);
    		 
    		$PresentRecordModel = M('present_record');
    		$result = $PresentRecordModel->where($where)->save(array('status' => 1));
    	
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			 
    			$this->success('success', U('present_record'));
    		} else {
    			$this->error('fail');
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			
    			$where = array('id' => $ids[0]);
    			$result = $PresentRecordModel->where($where)->save(array('status' => 1));
    		} else {
    			$where ['id'] = array('in', implode(',', $ids));
    			$result = $PresentRecordModel->where($where)->save(array('status' => 1));
    		}
    	
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    	
    		foreach ($ids as $id) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    		}
    		$this->ajaxReturn(array('status' => 1, 'url' => U('present_record')));
    	}
    }
}